package com.cater.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.School;
import com.cater.sys.service.impl.SchoolServiceImpl;

/**
 * @author yong
 * @Date 2018年3月22日-下午9:54:50
 * @学校管理控制层
 */
@Controller
@RequestMapping("/school")
public class SchoolController {

	@Autowired
	private SchoolServiceImpl service;

	/**
	 * @学校管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "sys/school/list";
	}

	/**
	 * @学校管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询学校分页数据")
	public PageResult<School> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<School> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("school:add")
	public String addView(Model model, CommonQuery query) {
		model.addAttribute("classify", query.getType());
		return "sys/school/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("school:add")
	@ResponseBody
	@SystemControllerLog(description = "添加学校")
	public JSONObject add(School t, CommonQuery query) {
		JSONObject json = new JSONObject();
		query.setName(t.getName());
		School count = service.getByName(query);
		if (count != null) {
			json.put("code", 999);
			json.put("message", "名称已存在");
		} else {
			String id = service.saves(t);
			if (StringUtils.isNotBlank(id)) {
				json.put("code", 200);
				json.put("id", id);
				json.put("message", "添加成功");
			} else {
				json.put("code", 999);
				json.put("message", "添加失败");
			}
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("school:edit")
	public String editView(String id, Model model, CommonQuery query) {
		School t = service.getById(id);
		model.addAttribute("school", t);
		return "sys/school/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("school:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改学校")
	public JSONObject update(School t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			School school = service.getById(t.getId());
			int count = 0;
			if (!t.getName().equals(school.getName())) {
				query.setName(t.getName());
				School scount = service.getByName(query);
				if (scount != null) {
					count = 1;
				}
			}
			if (count > 0) {
				json.put("code", 999);
				json.put("message", "名称已存在");
			} else {
				service.update(t);
				json.put("code", 200);
				json.put("message", "修改成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("school:delete")
	@SystemControllerLog(description = "删除学校")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	@ResponseBody
	@RequestMapping("/getJson")
	public List<School> getJson(CommonQuery query) {
		List<School> list = service.getAllCommon(query);
		return list;
	}

	@ResponseBody
	@RequestMapping("/getJsonSchool")
	public List<Map<String, Object>> getJsonCompany(CommonQuery query) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		String names = query.getName().replace("'", "");
		query.setName(names);
		List<School> schools = service.getAllCommon(query);
		for (School tSchool : schools) {
			map = new HashMap<String, Object>();
			map.put("id", tSchool.getId());
			map.put("value", tSchool.getName());
			list.add(map);
		}
		return list;
	}
}
